﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_UC_Chitietsanpham : System.Web.UI.UserControl
{
    BUS_Product data = new BUS_Product();
    static DataTable tbGioHang = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["GioHang"] != null)
            {
                tbGioHang = Session["GioHang"] as DataTable;
            }
            else
            {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("idSP", typeof(int));
                tbGioHang.Columns.Add("TenSP", typeof(string));
                tbGioHang.Columns.Add("Gia", typeof(double));
                tbGioHang.Columns.Add("SoLuong", typeof(int));
                tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
            }
            
            LoadData();
        }
    }
    public void LoadData()
    {
        string id = Request.QueryString["Id"];
        if (id!=null)
        {
            var dt = data.Product_GetById(id);
            Page.Title = dt.Rows[0]["Name"].ToString();
            ltrTitle.Text = dt.Rows[0]["Name"].ToString();
            var nsx = dt.Rows[0]["Manufacturer_Id"].ToString();
            ListView1.DataSource = dt;
            ListView1.DataBind();
            rptSanPham.DataSource = data.Product_GetByTop(0, "Manufacturer_Id = " + nsx+" and Id != "+id+" and [Status]=1", "[Order]");
            rptSanPham.DataBind();
            data.Product_Update(id, "[View] = [View]+1");
        }
        else
        {
            Response.Redirect("Sanpham.aspx");
        }
       
    }
    public string ThuongHieu(string Id)
    {
        BUS_Manufacturer nsx = new BUS_Manufacturer();
        var dt = data.Product_GetById(Id);
        var id_nsx = dt.Rows[0]["Manufacturer_Id"].ToString();
        return nsx.Manufacturer_GetByID(id_nsx).Rows[0]["Image"].ToString();
    }
    public void AddView(string Id)
    {
        data.Product_Update(Id, "[View] = [View]+1");
    }
    protected void rptSanPham_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "MuaHang")
        {
            if (Session["GioHang"] != null)
            {
                tbGioHang = Session["GioHang"] as DataTable;
            }
            else
            {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("idSP", typeof(int));
                tbGioHang.Columns.Add("TenSP", typeof(string));
                tbGioHang.Columns.Add("Gia", typeof(double));
                tbGioHang.Columns.Add("SoLuong", typeof(int));
                tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
            }
            int IdSp = int.Parse(e.CommandArgument.ToString());

            string strTenSP = ((Label)e.Item.FindControl("lblTenSanPham")).Text;
            float flGia = float.Parse(((Label)e.Item.FindControl("lblGia")).Text);
            int intSoLuong = 1;
            foreach (DataRow row in tbGioHang.Rows)
            {//Kiem tr neu mat hang da co roi thi tang so luong len 1
                if ((int)row["idSP"] == IdSp)
                {
                    row["SoLuong"] = (int)row["SoLuong"] + 1;
                    goto GioHang;
                }
            }
            tbGioHang.Rows.Add(IdSp, strTenSP, flGia, intSoLuong);
        GioHang:
            Session["GioHang"] = tbGioHang;


            Response.Redirect(Request.RawUrl);
        }

    }
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "MuaHang")
        {
            if (Session["GioHang"] != null)
            {
                tbGioHang = Session["GioHang"] as DataTable;
            }
            else
            {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("idSP", typeof(int));
                tbGioHang.Columns.Add("TenSP", typeof(string));
                tbGioHang.Columns.Add("Gia", typeof(double));
                tbGioHang.Columns.Add("SoLuong", typeof(int));
                tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
            }
            int IdSp = int.Parse(e.CommandArgument.ToString());

            string strTenSP = ((Label)e.Item.FindControl("lblTenSanPham")).Text;
            float flGia = float.Parse(((Label)e.Item.FindControl("lblGia")).Text);
            int intSoLuong = int.Parse(((TextBox)e.Item.FindControl("txtSoLuong")).Text);
            
            foreach (DataRow row in tbGioHang.Rows)
            {//Kiem tr neu mat hang da co roi thi tang so luong len 1
                if ((int)row["idSP"] == IdSp)
                {
                    row["SoLuong"] = (int)row["SoLuong"] + intSoLuong;
                    goto GioHang;
                }
            }
            tbGioHang.Rows.Add(IdSp, strTenSP, flGia, intSoLuong);
            GioHang:
            Session["GioHang"] = tbGioHang;


            Response.Redirect("~/GioHang.aspx");
        }
    }
}